const util = require('./utils.js')
//引入配置参数
const config = require('../config.js')
const app = getApp();

/**
 * AES与后台协商加密方式
 */
function AesForToken(params, aesKey) {
  return util.aesEncryptForToken(params, aesKey)
}

/**
 * 解密报文
 * @param {*} params 
 */
function AesDencryptParams(params, aesKey) {
  return util.aesDecrypt(params, aesKey)
}

/**
 * 
 * @param {*} url  url地址
 * @param {*} params  参数
 * @param {*} method   get/post
 * @param {*} onSuccess  成功
 * @param {*} onFailed  失败
 * @param {*} onCompleted 完成
 */

function request(url, params, method, onSuccess, onFailed, onCompleted) {
  console.log('请求url：' + url);
  console.log("请求参数:", params);
  wx.showLoading({
    title: '数据请求中...',
    mask: false
  })
  // console.log(url, "的请求时间：", new Date().getMinutes(), new Date().getSeconds(), new Date().getMilliseconds());
  wx.request({
    url: url,
    data: params,
    method: method,
    timeout: 60000,
    header: {
      'content-type': 'application/json',
      'tenant-id': 1,
      'Authorization': wx.getStorageSync('Token')
    },
    success: function (res) {
      wx.hideLoading()
      // console.log(url, "的响应时间：", new Date().getMinutes(), new Date().getSeconds(), new Date().getMilliseconds());
      console.log("返回码：", res.data.code);
      console.log("返回内容：", res.data.data);
      console.log("返回信息：", res.data.msg);
      if (res.data.code == 0) {
        if (!util.isEmpty(res.data)) {
          var data = res.data
          onSuccess(data);
        } else {
          util.showToast("系统开小差，请稍后再试")
        }
      } else {
        util.showToast(res.data.msg)
        if (res.data.code == '401' || res.data.code == '2020000003') {
          // 未登录或token过期
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/startPage/startPage',
            })
          }, 1500);
        }
      }
    },
    fail: function (error) {
      wx.hideLoading()
      console.log('请求失败：', error);
      onFailed(error);
    },
    complete: function (complete) {
      wx.hideLoading()
      if (onCompleted) {
        onCompleted(complete);
      }
    }
  })
}

/**
 * 供外部post请求调用  
 */
function postRequest(baseurl, url, params, onSuccess, onFailed, onCompleted) {
  request(baseurl + url, params, "POST", onSuccess, onFailed, onCompleted);
}

/**
 * 供外部delete请求调用  
 */
function deleteRequest(baseurl, url, params, onSuccess, onFailed, onCompleted) {
  request(baseurl + url, params, "delete", onSuccess, onFailed, onCompleted);
}

/**
 * 供外部PUT请求调用  
 */
function putRequest(baseurl, url, params, onSuccess, onFailed, onCompleted) {
  request(baseurl + url, params, "PUT", onSuccess, onFailed, onCompleted);
}

/**
 * 供外部get请求调用
 */
function getRequest(baseurl, url, params, onSuccess, onFailed, onCompleted) {
  request(baseurl + url, params, "GET", onSuccess, onFailed, onCompleted);
}


module.exports = {
  postRequest: postRequest,
  getRequest: getRequest,
  putRequest: putRequest,
  deleteRequest: deleteRequest,
  AesForToken: AesForToken
}